//
//  HRZShoppingCartManager.m
//  EMarket
//
//  Created by 石头 on 2018/5/23.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZShoppingCartManager.h"

// Controllers

// Models
#import "HRZGoodsModel.h"
// Views

// Vendors

// Categories
#import <FMDB.h>
// Others

@interface HRZShoppingCartManager ()
{
    FMDatabase *_db;
}

@end

@implementation HRZShoppingCartManager

+ (instancetype)shareDefaultManager {
    static HRZShoppingCartManager *_shoppingCartMgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shoppingCartMgr = [[HRZShoppingCartManager alloc] init];
        [_shoppingCartMgr initWithBase];
    });
    return _shoppingCartMgr;
}

- (void)initWithBase {
    /** document Path */
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    /** file Path */
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"shoppingCart.sqlite"];
    
    /** 实例化FMDataBase */
    _db = [FMDatabase databaseWithPath:filePath];
    
    /** 初始化table */
    [_db open];
    NSString *personSql = @"CREATE TABLE 'shoppingCart' ('id' INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL ,'categoryId' VARCHAR(255),'count' VARCHAR(255),'goodsId' VARCHAR(255),'goodsName' VARCHAR(255),'goodsType' VARCHAR(255),'orginalPrice' VARCHAR(255),'realPrice' VARCHAR(255),'shopId' VARCHAR(255),'storeName' VARCHAR(255),'total' VARCHAR(255),'sid' VARCHAR(255),'userId' VARCHAR(255),'imageURL' VARCHAR(255))";
    [_db executeUpdate:personSql];
    [_db close];
}

- (void)addShoppingCartGoods:(HRZGoodsModel *)goods {
    [_db open];
    
    [_db executeUpdate:@"INSERT INTO shoppingCart(categoryId,count,goodsId,goodsName,goodsType,orginalPrice,realPrice,shopId,storeName,total,sid,userId,imageURL)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)",goods.categoryId,goods.count,goods.goodsId,goods.goodsName,goods.goodsType,goods.orginalPrice,goods.realPrice,goods.shopId,goods.storeName,goods.total,goods.sid,goods.userId,goods.imageURL];
    
    [_db close];
}

- (void)removeShoppingCartGoods:(HRZGoodsModel *)goods {
    [_db open];
    
    [_db executeUpdate:@"DELETE FROM shoppingCart WHERE goodsId = ?",goods.goodsId];
    
    [_db close];
}

- (void)updateShoppingCartGoods:(HRZGoodsModel *)goods {
    [_db open];
    
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET categoryId = ?  WHERE goodsId = ? ",goods.categoryId,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET count = ?  WHERE goodsId = ? ",goods.count,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET goodsName = ?  WHERE goodsId = ? ",goods.goodsName,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET goodsType = ?  WHERE goodsId = ? ",goods.goodsType,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET orginalPrice = ?  WHERE goodsId = ? ",goods.orginalPrice,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET realPrice = ?  WHERE goodsId = ? ",goods.realPrice,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET shopId = ?  WHERE goodsId = ? ",goods.shopId,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET storeName = ?  WHERE goodsId = ? ",goods.storeName,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET total = ?  WHERE goodsId = ? ",goods.total,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET sid = ?  WHERE goodsId = ? ",goods.sid,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET userId = ?  WHERE goodsId = ? ",goods.userId,goods.goodsId];
    [_db executeUpdate:@"UPDATE 'shoppingCart' SET imageURL = ?  WHERE goodsId = ? ",goods.imageURL,goods.goodsId];
    
    [_db close];
}

- (NSArray *)allUserShoppingCartGoods {
    [_db open];
    
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    
    FMResultSet *res = [_db executeQuery:@"SELECT * FROM shoppingCart"];
    
    while ([res next]) {
        HRZGoodsModel *goods    = [[HRZGoodsModel alloc] init];
        goods.categoryId        = [res stringForColumn:@"categoryId"];
        goods.count             = [res stringForColumn:@"count"];
        goods.goodsId           = [res stringForColumn:@"goodsId"];
        goods.goodsName         = [res stringForColumn:@"goodsName"];
        goods.goodsType         = [res stringForColumn:@"goodsType"];
        goods.orginalPrice      = [res stringForColumn:@"orginalPrice"];
        goods.realPrice         = [res stringForColumn:@"realPrice"];
        goods.shopId            = [res stringForColumn:@"shopId"];
        goods.storeName          = [res stringForColumn:@"storeName"];
        goods.total             = [res stringForColumn:@"total"];
        goods.sid               = [res stringForColumn:@"sid"];
        goods.userId            = [res stringForColumn:@"userId"];
        goods.imageURL          = [res stringForColumn:@"imageURL"];
        
        [dataArray addObject:goods];
    }
    
    [_db close];
    
    return dataArray;
}

@end
